package service;

import dao.UserDao;
import entity.User;

import java.util.List;

public class UserService {
    private UserDao userDao = new UserDao();

    /**
     * 用户注册
     */
    public int register(User user){
        //TODO 合法性检验

        return userDao.add(user);
    }

    /**
     * 用户登录
     */
    public String login(String username, String password){
        String sql = "select id_number from user where user_name=? and password=?";
        List<User> users = userDao.query(sql, username, password);
        if(users==null || users.isEmpty()){
            return null;
        }else{
            return users.get(0).getIdNumber();
        }
    }

    /**
     * 查询所有用户的所有数据
     */
    public List<User> getAll(){
        return userDao.getAll();
    }

    /**
     * 通过用户名删除一个用户
     */
    public boolean deleteOneUser(String username){
        int cnt = userDao.delete(username);
        return cnt == 1;
    }
}
